package net.wanho.security.provider;

import net.wanho.security.token.JwtToken;
import net.wanho.util.JwtUtil;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.stereotype.Component;

/**
 * Author：汤小洋
 * Date：2023-11-13 16:45
 * Description：<描述>
 */
@Component
public class JwtProvider implements AuthenticationProvider {
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String token = (String) authentication.getPrincipal();
        // 判断token是否有效
        JwtUtil.valid(token);

        // 从token中获取用户名
        String username = JwtUtil.parseString(token, "username");
        // 获取用户权限信息

        return new JwtToken(token, AuthorityUtils.createAuthorityList("sys:user:list"));
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(JwtToken.class);
    }
}
